/******************************************************************************
    myBoard is a port of MacOS X dashboard application for windows.
    Copyright (C) 2007,2008  by Yann ALET
    Email : yann.alet@gmail.com

    myBoard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    myBard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using myBoard.Framework;
using System.IO;
using System.Reflection;
using System.Windows.Media.Animation;


namespace myBoard.CalendarNS
{
    public partial class Calendar : UserControl, IMagnet
    {
        private static Cal _cal;

        public static Cal Cal
        {
            get { return _cal; }
        }
        
        public Calendar()
        {
            InitializeComponent();
            SetBindings();
            _nextMonthBut.Click += new RoutedEventHandler(NextMonthBut_Click);
            _prevMonthBut.Click += new RoutedEventHandler(PrevMonthBut_Click);
        }

        void PrevMonthBut_Click(object sender, RoutedEventArgs e)
        {
            _cal.GotoPreviousMonth();
            SetBindings();
        }

        void NextMonthBut_Click(object sender, RoutedEventArgs e)
        {
            _cal.GotoNextMonth();
            SetBindings();
        }

        private void SetBindings()
        {
            if(_cal==null)
                _cal = new Cal();

            Binding calendarBinding = new Binding();
            calendarBinding.Source = _cal;
            _magnetRoot.SetBinding(Grid.DataContextProperty, calendarBinding);
        }

        void Calendar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            FlipRequested(MagnetSides.Front);
        }

        void Calendar_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            FlipRequested(MagnetSides.Back);
        }

        #region IMagnet Members

        public event OnFlipRequested FlipRequested;
        public event EventHandler CloseMyBoardRequested;

        public string MagnetName
        {
            get
            {
                return "Calendar";
            }
        }

        public BitmapImage GetLogoBitmapImage()
        {
            return ResourcesAccess.GetBitmapImageFromResouce("myBoard.Calendar.logo.png");
        }

        public BitmapImage GetThumbBitmapImage()
        {
            return ResourcesAccess.GetBitmapImageFromResouce("myBoard.Calendar.thumb.png");
        }

        public System.Drawing.Image GetLogoImage()
        {
            return ResourcesAccess.GetImageFromResouce("myBoard.Calendar.logo.png");
        }

        public System.Drawing.Image GetThumbImage()
        {
            return ResourcesAccess.GetImageFromResouce("myBoard.Calendar.thumb.png");
        }

        public Panel GetFrontSide()
        {
            return _frontside;
        }

        public Panel GetBackSide()
        {
            return _backside;
        }

        public Panel GetFlipZone()
        {
            return _flipZone;
        }       

        #endregion

        #region IMagnet Members


        public Assembly GetExecutingAssembly()
        {
            return Assembly.GetExecutingAssembly();
        }

        #endregion


        #region IMagnet Members


        public void Terminate()
        {
            //TODO Cleaning and saving here
        }

        #endregion
    }

    public class DayColorValueConvertor : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime date = (DateTime)value;
            if (date.DayOfYear == DateTime.Now.DayOfYear && date.Year == DateTime.Now.Year)
            {
                Color c = new Color();
                c.A = 255;
                c.R = 213;
                c.G = 252;
                c.B = 0;
                return new SolidColorBrush(c);

            }
            else if (date.Month != Calendar.Cal.ActiveDate.Month)
            {
                Color c = new Color();
                c.A = 255;
                c.R = 66;
                c.G = 66;
                c.B = 66;
                return new SolidColorBrush(c);
            }
            else
            {
                return Brushes.White;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }

        #endregion
    }
}